﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using HZ.DrugTrade.BLL;
using HZ.DrugTrade.Model;

namespace HZ.DrugTrade.Web
{
    public partial class OutTimeWarning : System.Web.UI.Page
    {
        BLL_Company com = new BLL_Company();
        BLL_SelectRelation selectRelation = new BLL_SelectRelation();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                string userid = CurrentAccount.GetUser().UserID;
                string type = Request.QueryString[0].ToString();
                if (type == "02")
                {
                    lblCmpID.Visible = false;
                    txtCmpID.Visible = false;
                    btnQuery.Visible = false;
                    lblCmpName.Visible = false;
                    txtCmpName.Visible = false;
                    string cmpSCIDs = selectRelation.GetCmpSCByCmpPSId(userid);
                    cmpSCIDs = cmpSCIDs + "'" + userid + "'";
                    BindDate(cmpSCIDs,"");
                }
                else if (type == "05")
                {
                    lblCmpID.Visible = false;
                    btnQuery.Visible = false;
                    txtCmpID.Visible = false;
                    lblCmpName.Visible = false;
                    txtCmpName.Visible = false;
                    userid = "'" + userid + "'";
                    BindDate(userid,"");
                }
                else if (type == "03")
                {

                    BindDate("","");
                }
            }

        }
        public void BindDate(string companyid,string companyname)
        {
            int totalRecord;
            gvwShowDate.DataSource = com.GetCmpTimeinfoByWhere(companyid, companyname, AspNetPager1.CurrentPageIndex, AspNetPager1.PageSize, out totalRecord);
            AspNetPager1.RecordCount = totalRecord;
            gvwShowDate.DataBind();

        }

        protected void gvwShowDate_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                for (int i = 2; i < e.Row.Cells.Count; i++)
                {
                    switch (e.Row.Cells[i].Text)
                    {
                        case "-1":
                            e.Row.Cells[i].Text = "已过期";
                            break;
                        case "91":
                            e.Row.Cells[i].Text = "未过期";
                            break;
                        case "-2":
                            e.Row.Cells[i].Text = "无效日期";
                            break;
                        default:
                            e.Row.Cells[i].Text = "还有" + e.Row.Cells[i].Text + "天";
                            break;
                    }
                }
            }
        }

        protected void btnQuery_Click(object sender, EventArgs e)
        {
            string cmpSCID="";
            if (txtCmpID.Text != "")
                cmpSCID = "'" + txtCmpID.Text.Trim() + "'";
            BindDate(cmpSCID, txtCmpName.Text);
        }

        protected void AspNetPager1_OnPageChanged(object sender, EventArgs e)
        {
            string userid = CurrentAccount.GetUser().UserID;
            string type = Request.QueryString[0].ToString();
            if (type == "02")
            {
                lblCmpID.Visible = false;
                txtCmpID.Visible = false;
                btnQuery.Visible = false;
                lblCmpName.Visible = false;
                txtCmpName.Visible = false;
                string cmpSCIDs = selectRelation.GetCmpSCByCmpPSId(userid);
                cmpSCIDs = cmpSCIDs + "'" + userid + "'";
                BindDate(cmpSCIDs, "");
            }
            else if (type == "05")
            {
                lblCmpID.Visible = false;
                btnQuery.Visible = false;
                txtCmpID.Visible = false;
                lblCmpName.Visible = false;
                txtCmpName.Visible = false;
                userid = "'" + userid + "'";
                BindDate(userid, "");
            }
            else if (type == "03")
            {

                BindDate("", "");
            }
        }

        protected void gvwShowDate_RowCreated(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType != DataControlRowType.DataRow) return;
            if (e.Row.FindControl("btn") != null)
            {
                LinkButton CtlButton = (LinkButton)e.Row.FindControl("btn");
                CtlButton.Click += new EventHandler(Pass_Click);
            }
        }


        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Pass_Click(object sender, EventArgs e)
        {
            //List<HZ.MBSM.Model.View.ProductGoods> hosList = new List<HZ.MBSM.Model.View.ProductGoods>();
            Button btn = (Button)sender;
            GridViewRow gvr = (GridViewRow)btn.Parent.Parent;
            string pk = gvwShowDate.DataKeys[gvr.RowIndex].Value.ToString();
            string a = gvwShowDate.Rows[gvr.RowIndex].Cells[0].Text;
            string companyid = "";
            for (int i = 0; i < gvwShowDate.Rows.Count; i++)
            {
                if (gvr.RowIndex == i)
                {
                    Label lblcompanyID = (Label)gvwShowDate.Rows[i].FindControl("lnlcompanyid");
                    if (!string.IsNullOrEmpty(lblcompanyID.Text))
                    {
                        companyid = lblcompanyID.Text;
                    }
                    //if (txtchkSpeciesProduction.Text.Equals("未审核"))
                    //{
                        //HZ.MBSM.Model.View.ProductGoods info = new HZ.MBSM.Model.View.ProductGoods();
                        //info.ChkSpeciesProduction = 1;
                        //info.GoodsId = Convert.ToInt64(pk);
                        //hosList.Add(info);
                    //}
                }
            }
            int flag = 0;
            try
            {
                if (!string.IsNullOrEmpty(companyid))
                {

                    //flag = com.;
                    //if (flag == 1)
                    //{
                    //    HZ.Web.WebUtil.Alert("审核通过", this);
                    //    BindData();
                    //}
                    //else
                    //{
                    //    HZ.Web.WebUtil.Alert("审核失败！", this);
                    //}
                }
                else
                {
                    HZ.Web.WebUtil.Alert("审核状态为”未审核“才可通过！", this);
                }
            }
            catch (Exception)
            {
                HZ.Web.WebUtil.Alert("审核失败！", this);
            }
        }
    }
}
